import bb.cascades 1.2
import bb.multimedia 1.2
import bb.WebImageView 1.4
import bb.data 1.0
import "lib"
import "controls"

Page {
    property variant item
    property string mp4Url
    actionBarVisibility: ChromeVisibility.Overlay
    
    function parseHtml(html) {
        html = html.replace(/width\=\"\d{0,}\"/g, "")
        html = html.replace(/height\=\"\d{0,}\"/g, "")
        html = html.replace(/<p>/g, "<p class='content'>")
        return html;
    }
    
    onItemChanged: {
        m_title.text = item.title
        m_source.text = qsTr("%1  %2").arg(item.updateTime).arg(item.source)
        
        video.visible = item.hasVideo
        video.preferredWidth = item.screenWidth
        videoView.preferredWidth = item.screenWidth
        
        dataSource.source = item.links[0].url;
        
        return;
        
        var baseUrl = item.links[0].url;
        com.get(baseUrl, function(data) {
                if (data.result) {
                    var json = JSON.parse(data.message);
                    m_body.html = parseHtml(json.body.text);

                    if(item.hasVideo){
                        videoView.url = json.body.videoPoster
                        mp4Url = json.body.videoSrc
                    }
                    
                } else {
                    _Setting.alert("加载出错...")
                }
        }, null)
    }
    
    ScrollView {
        Padding {
            Container {
                verticalAlignment: VerticalAlignment.Fill
                horizontalAlignment: HorizontalAlignment.Fill
                Container {
                    Label {
                        id: m_title
                        multiline: true
                        textStyle.base: com.titleStyle
                    }
                }
                
                Container {
                    Label {
                        id: m_source
                        textStyle.base: com.descStyle
                    }
                }
                
                Divider {
                }
                
                ProgressIndicator {
                    id: prog
                    fromValue: 0
                    toValue: 100
                    visible: value<100
                }
                
                Container {
                    id: video
                    layout: DockLayout {
                    }
                    
                    preferredHeight: 360*(preferredWidth/640)
                    preferredWidth: 0
                    
                    WebImageView{
                        id: videoView
                        preferredHeight: 360*(preferredWidth/640)
                        preferredWidth: 0
                        verticalAlignment: VerticalAlignment.Fill
                        horizontalAlignment: HorizontalAlignment.Fill
                    }
                    
                    ImageView {
                        imageSource: "asset:///images/ic_play.png"
                        verticalAlignment: VerticalAlignment.Center
                        horizontalAlignment: HorizontalAlignment.Center
                    }
                    
                    gestureHandlers: [
                        TapHandler {
                            onTapped: {
                                _Setting.inVoke("sys.mediaplayer.previewer", "bb.action.VIEW", "video/mp4", mp4Url, "")
                            }
                        }
                    ]
                }
                
                WebView {
                    id: m_body
                    verticalAlignment: VerticalAlignment.Fill
                    horizontalAlignment: HorizontalAlignment.Fill
                    implicitLayoutAnimationsEnabled: false
                    settings.credentialAutoFillEnabled: true
                    settings.formAutoFillEnabled: true
                    settings.activeTextEnabled: false
                    settings.viewport: {
                        "width": "device-width",
                        "initial-scale": 1.0
                    }
                    settings.userStyleSheetLocation: "asset:///controls/DocView.css"
                    onLoadProgressChanged: {
                        prog.value = loadProgress
                    }
                }
            }
            
            Container {
                preferredHeight: 50
            }
        }
      

    }
    
    attachedObjects: [
        Common {
            id: com
        },
        
        DataSource {
            id: dataSource
            onSourceChanged: {
                dataSource.load()
            }
            onDataLoaded: {
                m_body.html = parseHtml(data.body.text);
                
                if(item.hasVideo){
                    videoView.url = data.body.videoPoster
                    mp4Url = data.body.videoSrc
                }
            }
            
            onError: {
                _Setting.alert(qsTr("Load Error."))
            }
        }
    ]
}
